package it.unibo.refolding.pico.ui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.Line2D;

import javax.swing.JPanel;

public class PeakSlopesZDefPanel extends JPanel {

	private static final long serialVersionUID = 7795352732341921448L;

	private final ZDefDrawer drawer;
	final PeakSlopesHolder h;
	private final PeakSlopesDrawer peakSlopesDrawer;

	/**
	 * @param h
	 * @param peakSlopesDrawer
	 * @param contactPointZLsb Can be very different from z[offset] when the contact point it
	 * outside the rang of the curve (e.g., for refolding).
	 */
	public PeakSlopesZDefPanel(
	  ZDefDrawer drawer, PeakSlopesHolder h, PeakSlopesDrawer peakSlopesDrawer) {
		this.h = h;
		this.drawer = drawer;
		this.peakSlopesDrawer = peakSlopesDrawer;
		setPreferredSize(new Dimension(h.maxZ() - h.minZ(), h.maxDef() - h.minDef()));
	}

	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		super.paintComponent(g2);
		drawer.paintComponent(g2);
		Color savedColor = g2.getColor();
		Stroke savedStroke = g2.getStroke();
		g2.setColor(Color.BLACK);
		g2.setStroke(new BasicStroke(
			1.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0f, new float[] { 10, 10 },
			0.0f));
		g2.draw(new Line2D.Double(h.contactPointZLsb - h.minZ(), 0, h.contactPointZLsb - h.minZ(), h
			.maxDef() - h.minDef()));
		g2.setStroke(savedStroke);
		peakSlopesDrawer.paintComponent(g2);
		g2.setColor(savedColor);
	}

	public int minZ() {
		return h.minZ();
	}

	public int minDef() {
		return h.minDef();
	}
}
